﻿//===============================================================================
// Copyright © NextDocs Corporation 2006-2012.  All rights reserved.
//===============================================================================

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace LifeInSharePoint.Metro.Common.Controls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:MetroThemeControl runat=server></{0}:MetroThemeControl>")]
    public class MetroThemeControl : WebControl
    {

        private const string DEFAULTSITELOGO = "/Style Library/MetroUI/images/siteIcon.png";
        private const string DEFAULTSITECSS = "/Style Library/MetroUI/styles/styleColor.css";
        private const string METROACCORDIONJS = "/_layouts/LifeInSharePoint.Metro/js/MetroAccordion.js";
        private const string METROACCORDIONCLICKJS = "/_layouts/LifeInSharePoint.Metro/js/MetroAccordionClick.js";

        private bool GlobalStoredAccordionValue;
        private bool LocallyStoredAccordionValue;
        private bool activateAccordion;
        private bool GloballyStoredFloatedRibbon;
        private bool GlobalUseThemeIcon;
        private string GlobalQuickLaunchPosition;
        private string GlobalAccordionTypeValue;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();

            Guid siteId = SPContext.Current.Site.ID;
            Guid webId = SPContext.Current.Web.ID;


            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(siteId))
                using (SPWeb web = site.OpenWeb(webId))
                {
                    GlobalStoredAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(site.RootWeb, "GlobalMetroAccordionActivated"));
                    GloballyStoredFloatedRibbon = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(site.RootWeb, "GlobalMetroFloatedRibbon"));
                    GlobalUseThemeIcon = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(site.RootWeb, "GlobalUseThemeIcon"));

                    GlobalAccordionTypeValue = Utilities.SPPropertyBagHelper.GetPropertyValue(web, "GlobalMetroAccordionType");

                    GlobalQuickLaunchPosition = Utilities.SPPropertyBagHelper.GetPropertyValue(site.RootWeb, "GlobalMetroQuickLaunchPosition");
                    LocallyStoredAccordionValue = Convert.ToBoolean(Utilities.SPPropertyBagHelper.GetPropertyValue(web, "MetroAccordionActivated"));
                    activateAccordion = GlobalStoredAccordionValue || LocallyStoredAccordionValue;

                    ManageQuicklaunch(GlobalQuickLaunchPosition);
                    ManageTheme(web);
                    ManageAccordion(activateAccordion);
                    ManageRibbon();
                }
            });
        }

        private void ManageQuicklaunch(string globalQuickLaunchPosition)
        {
            if (globalQuickLaunchPosition != null)
                switch (globalQuickLaunchPosition)
                {
                    case "Left":
                        Controls.Add(new LiteralControl("<style>body #sideColumn{float:left;margin:0px 0px 0px 30px;}.rightpanel {margin:0px 20px 0px 220px !important; clear:right;}</style>"));
                        break;
                    case "Right":
                        Controls.Add(new LiteralControl("<style>body #sideColumn{float:right;margin:0px 20px 0px 0px;}.rightpanel {margin:0px 272px 0px 30px !important; clear:left;}</style>"));
                        break;
                }
        }



        private void ManageRibbon()
        {
            if (GloballyStoredFloatedRibbon)
            {
                //If Ribbon is to be floated then override theme style.
                this.Controls.Add(new LiteralControl("<style>body #ribbonWrapper {  position: fixed; top:0px; z-index:1000; width:100%; }#s4-bodyContainer {margin-top:80px;}</style>"));
            }
        }

        private void ManageAccordion(bool StoredAccordionValue)
        {
            if (StoredAccordionValue)
            {
                Controls.Add(GlobalAccordionTypeValue == "Click"
                                      ? new LiteralControl("<script type='text/javascript' src='" +
                                                           SPContext.Current.Web.Url + METROACCORDIONCLICKJS +
                                                           "'></script>")
                                      : new LiteralControl("<script type='text/javascript' src='" +
                                                           SPContext.Current.Web.Url + METROACCORDIONJS + "'></script>"));
            }
        }

        private void ManageTheme(SPWeb web)
        {
            try
            {
                Controls.Add(new LiteralControl("<link rel='stylesheet' type='text/css' href='" + SPContext.Current.Site.Url + DEFAULTSITECSS + "'/>"));

                if (GlobalUseThemeIcon)
                {
                    web.AllowUnsafeUpdates = true;
                    web.SiteLogoUrl = SPContext.Current.Site.Url + "/" + DEFAULTSITELOGO;
                    web.Update();
                }  
            }
            catch (Exception ex)
            {
                Label msg = new Label {Text = ex.Message};
                Controls.Add(msg);
            }
        }
    }
}
